#include "capcontrolbarwidget.h"

QCAPParamBarWidget::QCAPParamBarWidget(QWidget *parent) : QWidget(parent)
{
    _pCapControl = CCapControl::getInstance();

    st_widgetAttr l_st_widgetAttr;
    _pGroundTestWidget = new QGroundTestWidget(this);
    connect(_pGroundTestWidget,&QGroundTestWidget::signal_startTest,this,&QCAPParamBarWidget::onGroundTest);
    l_st_widgetAttr.pwidget = _pGroundTestWidget;
    l_st_widgetAttr.sName = "地面测试";
    m_st_widgetlist.append(l_st_widgetAttr);



    _pAxisControlWidget[M_AXIS_AZI] = new QAxisWorkModeWidget(0,this);
    connect(_pAxisControlWidget[M_AXIS_AZI],&QAxisWorkModeWidget::signal_setparam,this,&QCAPParamBarWidget::onSetPosAndSpeedParam);
    l_st_widgetAttr.pwidget = _pAxisControlWidget[M_AXIS_AZI];
    l_st_widgetAttr.sName = "位置/速度给定模式-方位轴";
    m_st_widgetlist.append(l_st_widgetAttr);

    _pAxisControlWidget[M_AXIS_PICTH] = new QAxisWorkModeWidget(1,this);
    connect(_pAxisControlWidget[M_AXIS_PICTH],&QAxisWorkModeWidget::signal_setparam,this,&QCAPParamBarWidget::onSetPosAndSpeedParam);
    l_st_widgetAttr.pwidget = _pAxisControlWidget[M_AXIS_PICTH];
    l_st_widgetAttr.sName = "位置/速度给定模式-俯仰轴";
    m_st_widgetlist.append(l_st_widgetAttr);


    _pAxisThresholdWidget[M_AXIS_AZI] = new QAxisThresholdWidget(0,this);
    connect(_pAxisThresholdWidget[M_AXIS_AZI],&QAxisThresholdWidget::signal_setparam,this,&QCAPParamBarWidget::onSetPosAndSpeedParam);
    l_st_widgetAttr.pwidget = _pAxisThresholdWidget[M_AXIS_AZI];
    l_st_widgetAttr.sName = "门限值设置-方位轴";
    m_st_widgetlist.append(l_st_widgetAttr);

    _pAxisThresholdWidget[M_AXIS_PICTH] = new QAxisThresholdWidget(1,this);
    connect(_pAxisThresholdWidget[M_AXIS_PICTH],&QAxisThresholdWidget::signal_setparam,this,&QCAPParamBarWidget::onSetPosAndSpeedParam);
    l_st_widgetAttr.pwidget = _pAxisThresholdWidget[M_AXIS_PICTH];
    l_st_widgetAttr.sName = "门限值设置-俯仰轴";
    m_st_widgetlist.append(l_st_widgetAttr);


    _pAxisparamwidget[M_AXIS_AZI] = new QAxisPIDWidget(0,"方位轴",this);
    connect(_pAxisparamwidget[M_AXIS_AZI],&QAxisPIDWidget::signal_setpid,this,&QCAPParamBarWidget::onSetAxisParam);
    l_st_widgetAttr.pwidget = _pAxisparamwidget[M_AXIS_AZI];
    l_st_widgetAttr.sName = "PID参数—方位轴";
    m_st_widgetlist.append(l_st_widgetAttr);

    _pAxisparamwidget[M_AXIS_PICTH] = new QAxisPIDWidget(1,"俯仰轴",this);
    connect(_pAxisparamwidget[M_AXIS_PICTH],&QAxisPIDWidget::signal_setpid,this,&QCAPParamBarWidget::onSetAxisParam);
    l_st_widgetAttr.pwidget = _pAxisparamwidget[M_AXIS_PICTH];
    l_st_widgetAttr.sName = "PID参数-俯仰轴";
    m_st_widgetlist.append(l_st_widgetAttr);

    QUnLockCmdWidget* punlockwidget = new QUnLockCmdWidget(this);
    connect(punlockwidget,&QUnLockCmdWidget::signal_unlockcmd,this,&QCAPParamBarWidget::onSetUnlockCmd);
    l_st_widgetAttr.pwidget = punlockwidget;
    l_st_widgetAttr.sName = "解锁";
    m_st_widgetlist.append(l_st_widgetAttr);

    QAxisHandShakeWidget* phandshakewidget = new QAxisHandShakeWidget(0,"方位轴",this);
    connect(phandshakewidget,&QAxisHandShakeWidget::signal_sethandshake,this,&QCAPParamBarWidget::onSetAixsHandShakeParam);
    l_st_widgetAttr.pwidget = phandshakewidget;
    l_st_widgetAttr.sName = "方位握手";
    m_st_widgetlist.append(l_st_widgetAttr);

    phandshakewidget = new QAxisHandShakeWidget(1,"俯仰轴",this);
    connect(phandshakewidget,&QAxisHandShakeWidget::signal_sethandshake,this,&QCAPParamBarWidget::onSetAixsHandShakeParam);
    l_st_widgetAttr.pwidget = phandshakewidget;
    l_st_widgetAttr.sName = "俯仰握手";
    m_st_widgetlist.append(l_st_widgetAttr);


    InitUI();
}
/**
* @brief
* @note
*
*/
void QCAPParamBarWidget::InitUI()
{
    _pScrollAreaWidget = new QScrollArea(this);

    QWidget* pwidget = new QWidget(this);
    QVBoxLayout* vlayout = new QVBoxLayout(pwidget);
    vlayout->addSpacerItem(new QSpacerItem(10,100,QSizePolicy::Expanding,QSizePolicy::Expanding));

    QLabel* plabel = new QLabel("控制及参数设置",this);
    plabel->setStyleSheet("QLabel{color:rgb(255,255,255);font-family:'Microsoft YaHei';font-size:24px;}");
    plabel->setAlignment(Qt::AlignCenter);
    vlayout->addWidget(plabel);


    for(int i = 0;i < m_st_widgetlist.size();i++)
    {
        st_widgetAttr l_st_widgetAttr = m_st_widgetlist[i];

        QDrawerButton* pButton = new QDrawerButton(this);
        pButton->setMinimumHeight(40);
        m_st_buttonlist.append(pButton);

        pButton->setObjectName(l_st_widgetAttr.sName);
        QString sStyle = QString("QLabel{color:rgb(230,230,230);}"
                                 "QPushButton{background-color:rgba(195,195,195,0.4)}"
                                 "QPushButton:hover{background-color:rgba(195,195,195,0.4)}"
                                 "QPushButton:pressed{background-color:rgba(127,127,127,0.4)}");
        pButton->setStyleSheet(sStyle);
        pButton->SetImageLabel(">");
        pButton->SetTextLabel(l_st_widgetAttr.sName);

        l_st_widgetAttr.pwidget->setObjectName(l_st_widgetAttr.sName+"_widget");
        vlayout->addWidget(pButton);
        vlayout->addWidget(l_st_widgetAttr.pwidget);

        l_st_widgetAttr.pwidget->setHidden(true);
        vlayout->setMargin(0);

        connect(pButton,&QDrawerButton::clicked,this,&QCAPParamBarWidget::onAttrBtnClick);
    }

    vlayout->addSpacerItem(new QSpacerItem(10,800,QSizePolicy::Expanding,QSizePolicy::Expanding));


    //默认第一个打开
    m_st_buttonlist[0]->SetImageLabel("▾");
    m_st_widgetlist[0].pwidget->setHidden(false);


    _pScrollAreaWidget->setWidget(pwidget);
    _pScrollAreaWidget->setWidgetResizable(true);

    QGridLayout* mylayout = new QGridLayout(this);
    mylayout->addWidget(_pScrollAreaWidget);

}

/**
* @brief 隐藏/显示属性编辑窗口
* @note
*
*/
void QCAPParamBarWidget::onAttrBtnClick(bool checked)
{
    QDrawerButton* pButton =  static_cast<QDrawerButton*>(this->sender());
    QString objName = static_cast<QString>(this->sender()->objectName());
    QWidget* pObj = this->findChild<QWidget*>(objName+"_widget");
    pObj->setHidden(!pObj->isHidden());

    if(pObj->isHidden())
    {
        pButton->SetImageLabel(">");
    }
    else
    {
        pButton->SetImageLabel("▾");
    }

}


/**
* @brief 设置pid参数
* @note
*
*/
void QCAPParamBarWidget::onSetAxisParam(int i32AxisNo,int nType,float lfkp,float lfki,float lfkd)
{
    _pCapControl->setAxisParam(i32AxisNo,nType,lfkp,lfki,lfkd);
    qDebug()<<"设置PID参数:"<<i32AxisNo<<nType<<lfkp<<lfki<<lfkd;
}
/**
* @brief
* @note
*
*/
void QCAPParamBarWidget::onSetAixsHandShakeParam(int i32AxisNo,int i32value,int datalen)
{
    _pCapControl->setAxisHandShakeParam(i32AxisNo,i32value,datalen);
    qDebug()<<"握手信号:"<<i32AxisNo<<i32value;
}

/**
* @brief
* @note
*
*/
void QCAPParamBarWidget::onSetUnlockCmd(int cmd,int value)
{
    _pCapControl->setUnlockCmd(cmd,value);
    qDebug()<<"解锁指令:"<<cmd;
}

/**
* @brief 速度给定模式或位置给定模式参数设置
* @note
*
*/
void QCAPParamBarWidget::onSetPosAndSpeedParam(int i32AxisNo,int i32Cmdid,int i32Value,int datalen)
{
    _pCapControl->setPosAndSpeedParam(i32AxisNo,i32Cmdid,i32Value,datalen);
    qDebug()<<"onSetPosAndSpeedParam:"<<i32AxisNo<<i32Cmdid<<i32Value<<datalen;
}
/**
* @brief
* @note
*
*/
void QCAPParamBarWidget::onGroundTest(int i32Axis,int workmode, int data0,int data1)
{
    _pCapControl->startGroudTest(i32Axis,workmode,data0,data1);
}

/**
* @brief 接收的数据
* @note
*
*/
void QCAPParamBarWidget::onRcvData(QVariant var)
{
    st_rcvdata l_rcv_data = var.value<st_rcvdata>();

    //显示反馈数据 AA帧为pid 参数反馈
    if(l_rcv_data.loopcmd == 0xAA)
    {
        _pAxisparamwidget[M_AXIS_AZI]->showRcvdata(l_rcv_data.loopcmd_data.loopdata_AA);
        _pAxisparamwidget[M_AXIS_PICTH]->showRcvdata(l_rcv_data.loopcmd_data.loopdata_AA);
    }
    if(l_rcv_data.loopcmd == 0x11)
    {
        _pAxisControlWidget[M_AXIS_AZI]->showRcvdata11(l_rcv_data.loopcmd_data.loopdata_11);
        _pAxisControlWidget[M_AXIS_PICTH]->showRcvdata11(l_rcv_data.loopcmd_data.loopdata_11);

        _pAxisThresholdWidget[M_AXIS_AZI]->showRcvdata11(l_rcv_data.loopcmd_data.loopdata_11);
        _pAxisThresholdWidget[M_AXIS_PICTH]->showRcvdata11(l_rcv_data.loopcmd_data.loopdata_11);
    }


    if(l_rcv_data.loopcmd == 0x55)
    {
        _pAxisThresholdWidget[M_AXIS_AZI]->showRcvdata55(l_rcv_data.loopcmd_data.loopdata_55);
        _pAxisThresholdWidget[M_AXIS_PICTH]->showRcvdata55(l_rcv_data.loopcmd_data.loopdata_55);
    }

    if(l_rcv_data.loopcmd == 0xBB)
    {
        _pGroundTestWidget->StopTest();
    }



}
